package com.tl.bootmeter.controller;

import com.tl.bootmeter.pojo.MeterValue;
import com.tl.bootmeter.pojo.WellMeter;
import com.tl.bootmeter.pojo.WellMeterValue;
import com.tl.bootmeter.service.IMeterValueService;
import com.tl.bootmeter.service.IWellModeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("metervalue")
public class MeterValueController {

    @Autowired
    private IMeterValueService meterValueServiceImpl;
    @Autowired
    private IWellModeService wellModeServiceImpl;
    /**
     * 根据井号添加仪表基础信息
     * @param station_id
     * @param meter_number
     * @param meter_name
     * @param meter_manufactor
     * @param meter_network_type
     * @param well_namber
     * @param well_location
     * @return 返回一个int如果为0，添加失败，1添加成功
     */
    @RequestMapping("insertByMeterValue.do")
    public ModelAndView insertByMeterValue(int station_id, String meter_number, String meter_name, String meter_manufactor, int meter_network_type, String well_namber, String well_location){
        System.out.println(" 根据井号添加仪表基础信息="+station_id);
        //封装对象仪表基础信息表
        MeterValue meterValue = new MeterValue();
        meterValue.setMeter_number(meter_number);
        meterValue.setMeter_name(meter_name);
        meterValue.setMeter_manufactor(meter_manufactor);
        meterValue.setMeter_network_type(meter_network_type);
        System.out.println("----"+well_namber+"--------"+meter_number);
        //封装对象井仪表中间表
        WellMeter wellMeter = new WellMeter();
        wellMeter.setWell_nanber(well_namber);
        wellMeter.setWell_location(well_location);
        wellMeter.setMeter_sn(meter_number);
        int end = meterValueServiceImpl.insertByMeterValue(meterValue, wellMeter);
        //重新调用按照站id查询有多少口井并返回当前页面
        Map<String, Object> stationIdWellModeMap = wellModeServiceImpl.selectBystationIdWellMode(station_id);
        ModelAndView mo = new ModelAndView();
        mo.addObject("stationIdWellModeMap",stationIdWellModeMap);
        mo.setViewName("well_tables");
        return mo;
    }

    /**
     * 根据仪表编号查询metervalue表中是否存在
     * @param meter_sn 页面传入的仪表编号
     * @return 返回一个metervalue对象
     */
    @RequestMapping("selectMeterValueByMeterSn.do")
    @ResponseBody
    public Boolean selectMeterValueByMeterSn(String meter_sn){
        System.out.println("meter_sn="+meter_sn);
        MeterValue metervalue = meterValueServiceImpl.selectMeterValueByMeterSn(meter_sn);
        System.out.println(metervalue);
        if(metervalue != null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 根据井号查询这个井下共有多少块表
     * @param well_namber(页面获取到的井号传入)
     * @return 返回一个对象，对象内封装的list
     */
    @RequestMapping("selectBywellNamber.do")
    public ModelAndView selectBywellNamber(String well_namber){
        System.out.print("页面传入的井号="+well_namber);
        ModelAndView modelAndView = new ModelAndView();
        List<WellMeterValue> wellMeterList = meterValueServiceImpl.selectBywellNamber(well_namber);
        System.out.println("查询这个井下所有的仪表信息="+wellMeterList);
        modelAndView.addObject("wellMeterList", wellMeterList);
        modelAndView.setViewName("meter_tables");
        return modelAndView;
    }
}
